/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import filters.TIOAFilter;
import filters.TIOATxtFilter;
import gui.incremental.JTIOADialog;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintStream;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import org.apache.commons.io.FileUtils;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import parameters.Parameters;
import parsing.Parser;
import parsing.TokenMgrError;
import semantics.TIOARuntime;
import semantics.reachability.ExhaustiveReachabilityAnalysis;
import semantics.reachability.ReachabilityAnalysis;
import syntax.Clock;
import syntax.Location;
import syntax.Repository;
import syntax.TIOA;
import test.MinDelayTestStrategy;
import test.TestStrategy;
import xmlparser.XMLParser;
import xmlparser.XMLParsingException;

/**
 *
 * @author hicham
 */
public class Gui extends javax.swing.JFrame {

    TIOA tioa;
    Set<JMenuItem> menus = new HashSet<JMenuItem>();

    /**
     * Creates new form Gui
     */
    public Gui() {
        initComponents();
        initFieldsValues();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrameOpenFile = new javax.swing.JFrame();
        jOpenFileChooser = new javax.swing.JFileChooser();
        jToolParametersFrame = new javax.swing.JFrame();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jEpsilonTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jInfinityTextField = new javax.swing.JTextField();
        jOKToolParametersButton = new javax.swing.JButton();
        jCancelToolParametersButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jTIOAScrollPane = new javax.swing.JScrollPane();
        jEditorTextArea = new javax.swing.JTextArea();
        jOutputInternalFrame = new javax.swing.JInternalFrame();
        jScrollPane2 = new javax.swing.JScrollPane();
        jOutputTextArea = new javax.swing.JTextArea();
        jOutputSaveButton = new javax.swing.JButton();
        jOutputCloseButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenuFile = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        exitMenuItem = new javax.swing.JMenuItem();
        jMenuEdit = new javax.swing.JMenu();
        parametersMenuItem = new javax.swing.JMenuItem();
        jMenuTest = new javax.swing.JMenu();
        jExhaustiveTestingMenuItem = new javax.swing.JMenuItem();
        jIncrementalTestingMenuItem = new javax.swing.JMenuItem();
        jMenuVerification = new javax.swing.JMenu();
        reachabilityMenuItem = new javax.swing.JMenuItem();
        jMenuLanguage = new javax.swing.JMenu();
        jMenuHelp = new javax.swing.JMenu();
        jHelpAboutMenuItem = new javax.swing.JMenuItem();

        javax.swing.GroupLayout jFrameOpenFileLayout = new javax.swing.GroupLayout(jFrameOpenFile.getContentPane());
        jFrameOpenFile.getContentPane().setLayout(jFrameOpenFileLayout);
        jFrameOpenFileLayout.setHorizontalGroup(
            jFrameOpenFileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jFrameOpenFileLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jOpenFileChooser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jFrameOpenFileLayout.setVerticalGroup(
            jFrameOpenFileLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jFrameOpenFileLayout.createSequentialGroup()
                .addComponent(jOpenFileChooser, javax.swing.GroupLayout.DEFAULT_SIZE, 453, Short.MAX_VALUE)
                .addContainerGap())
        );

        jToolParametersFrame.setBounds(new java.awt.Rectangle(0, 0, 300, 230));
        jToolParametersFrame.setResizable(false);

        jLabel1.setText("Epsilon");

        jLabel2.setText("Infinity");

        jInfinityTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jInfinityTextFieldActionPerformed(evt);
            }
        });

        jOKToolParametersButton.setText("OK");
        jOKToolParametersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOKToolParametersButtonActionPerformed(evt);
            }
        });

        jCancelToolParametersButton.setText("Cancel");
        jCancelToolParametersButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCancelToolParametersButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Cantarell", 1, 18)); // NOI18N
        jLabel3.setText("Tool parameters");
        jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jOKToolParametersButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jCancelToolParametersButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jEpsilonTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jInfinityTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(30, 30, 30))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap(54, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jEpsilonTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jInfinityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jOKToolParametersButton)
                    .addComponent(jCancelToolParametersButton))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jToolParametersFrameLayout = new javax.swing.GroupLayout(jToolParametersFrame.getContentPane());
        jToolParametersFrame.getContentPane().setLayout(jToolParametersFrameLayout);
        jToolParametersFrameLayout.setHorizontalGroup(
            jToolParametersFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jToolParametersFrameLayout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );
        jToolParametersFrameLayout.setVerticalGroup(
            jToolParametersFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        jToolParametersFrame.getAccessibleContext().setAccessibleParent(this);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jEditorTextArea.setColumns(20);
        jEditorTextArea.setRows(5);
        jTIOAScrollPane.setViewportView(jEditorTextArea);

        jOutputInternalFrame.setVisible(false);

        jOutputTextArea.setColumns(20);
        jOutputTextArea.setRows(5);
        jScrollPane2.setViewportView(jOutputTextArea);

        jOutputSaveButton.setText("Save");
        jOutputSaveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOutputSaveButtonActionPerformed(evt);
            }
        });

        jOutputCloseButton.setText("Close");
        jOutputCloseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jOutputCloseButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jOutputInternalFrameLayout = new javax.swing.GroupLayout(jOutputInternalFrame.getContentPane());
        jOutputInternalFrame.getContentPane().setLayout(jOutputInternalFrameLayout);
        jOutputInternalFrameLayout.setHorizontalGroup(
            jOutputInternalFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(jOutputInternalFrameLayout.createSequentialGroup()
                .addGap(130, 130, 130)
                .addComponent(jOutputSaveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jOutputCloseButton)
                .addGap(143, 143, 143))
        );
        jOutputInternalFrameLayout.setVerticalGroup(
            jOutputInternalFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jOutputInternalFrameLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 349, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jOutputInternalFrameLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jOutputSaveButton)
                    .addComponent(jOutputCloseButton))
                .addGap(0, 0, Short.MAX_VALUE))
        );

        jMenuFile.setText("File");

        newMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        newMenuItem.setText("New");
        newMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newMenuItemActionPerformed(evt);
            }
        });
        jMenuFile.add(newMenuItem);

        openMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        openMenuItem.setText("Open");
        openMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenuItemActionPerformed(evt);
            }
        });
        jMenuFile.add(openMenuItem);

        saveMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        saveMenuItem.setText("Save");
        jMenuFile.add(saveMenuItem);

        exitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        exitMenuItem.setText("Exit");
        exitMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitMenuItemActionPerformed(evt);
            }
        });
        jMenuFile.add(exitMenuItem);

        jMenuBar1.add(jMenuFile);

        jMenuEdit.setText("Edit");

        parametersMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_P, java.awt.event.InputEvent.CTRL_MASK));
        parametersMenuItem.setText("Parameters");
        parametersMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                parametersMenuItemActionPerformed(evt);
            }
        });
        jMenuEdit.add(parametersMenuItem);

        jMenuBar1.add(jMenuEdit);

        jMenuTest.setText("Test");

        jExhaustiveTestingMenuItem.setText("Exhaustive Testing");
        jExhaustiveTestingMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jExhaustiveTestingMenuItemActionPerformed(evt);
            }
        });
        jMenuTest.add(jExhaustiveTestingMenuItem);

        jIncrementalTestingMenuItem.setText("Incremental Testing");
        jIncrementalTestingMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jIncrementalTestingMenuItemActionPerformed(evt);
            }
        });
        jMenuTest.add(jIncrementalTestingMenuItem);

        jMenuBar1.add(jMenuTest);

        jMenuVerification.setText("Verification");

        reachabilityMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R, java.awt.event.InputEvent.CTRL_MASK));
        reachabilityMenuItem.setText("Reachability");
        reachabilityMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reachabilityMenuItemActionPerformed(evt);
            }
        });
        jMenuVerification.add(reachabilityMenuItem);

        jMenuBar1.add(jMenuVerification);

        jMenuLanguage.setText("Language");
        jMenuBar1.add(jMenuLanguage);

        jMenuHelp.setText("Help");

        jHelpAboutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_B, java.awt.event.InputEvent.CTRL_MASK));
        jHelpAboutMenuItem.setText("About");
        jHelpAboutMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jHelpAboutMenuItemActionPerformed(evt);
            }
        });
        jMenuHelp.add(jHelpAboutMenuItem);

        jMenuBar1.add(jMenuHelp);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jOutputInternalFrame, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTIOAScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 308, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTIOAScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE)
            .addComponent(jOutputInternalFrame, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void newMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newMenuItemActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_newMenuItemActionPerformed

    private void reachabilityMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reachabilityMenuItemActionPerformed
        // TODO add your handling code here:
//        StringBuilder stb = new StringBuilder();
//        stb.append(tioa.printTIOA());
//        
//        stb.append("\n**********************\n");
//        stb.append("Reachability Analysis\n");
//        stb.append("**********************\n");
//        
//        TestStrategy testStrategy = new MinDelayTestStrategy();
//        stb.append(testStrategy.test(tioa));

        ReachabilityAnalysis reachabilityAnalysis = new ExhaustiveReachabilityAnalysis();
        TIOARuntime tioaRuntime = reachabilityAnalysis.analyse(tioa);

        jOutputTextArea.setText(tioaRuntime.printReachability());
//        jOutputTextArea.setText(tioa.printTIOA());
        jTIOAScrollPane.setVisible(false);
        jOutputInternalFrame.setVisible(true);
        jOutputInternalFrame.show();
    }//GEN-LAST:event_reachabilityMenuItemActionPerformed

    private void openMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openMenuItemActionPerformed
        // TODO add your handling code here:
        boolean nfromxml = true;
        try {
            jFrameOpenFile.setTitle("Open File");
            jOpenFileChooser.showDialog(jFrameOpenFile, "Open File");

            jOpenFileChooser.setFileFilter(new TIOATxtFilter());

            if (jOpenFileChooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION) {
                return;
            }
            File sourceFile = jOpenFileChooser.getSelectedFile();
            if (TIOAFilter.getExtension(sourceFile).equals("xml")) {
                try {
                    System.out.println(System.getProperty("user.dir"));
                    String tmpDir = FileUtils.getTempDirectoryPath() + File.separator + "xsd";
                    File localCopy = new File(tmpDir, sourceFile.getName());
                    FileUtils.copyFile(sourceFile, localCopy);
                    ClassLoader clsLoad = this.getClass().getClassLoader();
                    InputStream xsdInputStream = clsLoad.getResourceAsStream("resources/system.xsd");
                    File xsdFile = new File(tmpDir, "system.xsd");
                    FileUtils.copyInputStreamToFile(xsdInputStream, xsdFile);
                    XMLParser instance = new XMLParser(localCopy);
                    tioa = instance.parse();
                } catch (XMLParsingException e) {
                
                }
//                try {
//                    nfromxml = false;
//                    jEditorTextArea.setText(fromXMLToString(fichier));
//                } catch (JDOMException ex) {
//                    JOptionPane.showMessageDialog(this, ex.toString(), TraductionPref.getMSG("ouvrir.probfich"), JOptionPane.ERROR_MESSAGE);
//                }
                setMenusEnabled(true);
            } 
//            else if (TIOAFilter.getExtension(fichier).equals("tioa")) {
//                try {
//                    FileInputStream fis = new FileInputStream(fichier);
//                    ObjectInputStream ob = new ObjectInputStream(fis);
//                    tioa = (TIOA) ob.readObject();
//                } catch (Exception ex) {
//                    JOptionPane.showMessageDialog(this, ex.toString(), TraductionPref.getMSG("ouvrir.probfich"), JOptionPane.ERROR_MESSAGE);
//                    return;
//                }
//            } 
            else {
                try {
                    tioa = Parser.open(sourceFile);
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, TraductionPref.getMSG("ouvrir.erreursyntax"),
                            jOpenFileChooser.getSelectedFile().getName(), JOptionPane.ERROR_MESSAGE);
                    return;
                } catch (TokenMgrError e) {
                    JOptionPane.showMessageDialog(this, TraductionPref.getMSG("ouvrir.erreursyntax"),
                            jOpenFileChooser.getSelectedFile().getName(), JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
            //this.affichDetails.setEnabled(true);
            //this.testSystem.setEnabled(true);
            //this.modifierSystem.setEnabled(true);
            jMenuVerification.setEnabled(true);
            //enregistrer.setEnabled(true);
            //modifier.setEnabled(true);
            jMenuTest.setEnabled(true);
            if (nfromxml) {
                jEditorTextArea.setText(null);
                writeInto(jEditorTextArea, tioa);
            } else {
                //fromEditeurToSys();
            }
            setMenusEnabled(true);
        } catch (IOException ex) {
            //Logger.getLogger(Affich.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_openMenuItemActionPerformed

    private void parametersMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_parametersMenuItemActionPerformed
        // TODO add your handling code here:
        jToolParametersFrame.setVisible(true);
    }//GEN-LAST:event_parametersMenuItemActionPerformed

    private void jInfinityTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jInfinityTextFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jInfinityTextFieldActionPerformed

    private void jOKToolParametersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOKToolParametersButtonActionPerformed
        // TODO add your handling code here:
        Float epsilon, infinity;
        try {
            epsilon = Float.parseFloat(jEpsilonTextField.getText());
            infinity = Float.parseFloat(jInfinityTextField.getText());
            Parameters.setEpsilon(epsilon);
            Parameters.setInfinity(infinity);
        } catch (Exception e) {
        }
        jToolParametersFrame.setVisible(false);
    }//GEN-LAST:event_jOKToolParametersButtonActionPerformed

    private void jCancelToolParametersButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCancelToolParametersButtonActionPerformed
        // TODO add your handling code here:
        jToolParametersFrame.setVisible(false);
    }//GEN-LAST:event_jCancelToolParametersButtonActionPerformed

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitMenuItemActionPerformed
        // TODO add your handling code here:
        exit();
    }//GEN-LAST:event_exitMenuItemActionPerformed

    private void jHelpAboutMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jHelpAboutMenuItemActionPerformed
        // TODO add your handling code here:
        about();
    }//GEN-LAST:event_jHelpAboutMenuItemActionPerformed

    private void jOutputSaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOutputSaveButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jOutputSaveButtonActionPerformed

    private void jOutputCloseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jOutputCloseButtonActionPerformed
        // TODO add your handling code here:
        //jOutputInternalFrame.hide();
        jOutputInternalFrame.setVisible(false);
        jTIOAScrollPane.setVisible(true);
    }//GEN-LAST:event_jOutputCloseButtonActionPerformed

    private void jIncrementalTestingMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jIncrementalTestingMenuItemActionPerformed
        // TODO add your handling code here:
        JTIOADialog frame = new JTIOADialog(tioa);
        frame.setVisible(true);
    }//GEN-LAST:event_jIncrementalTestingMenuItemActionPerformed

    private void jExhaustiveTestingMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jExhaustiveTestingMenuItemActionPerformed
        // TODO add your handling code here:
        TestStrategy testStrategy = new MinDelayTestStrategy();
        //System.out.println(testStrategy.test(tioa));
        jOutputTextArea.setText(testStrategy.test(tioa));
        jTIOAScrollPane.setVisible(false);
        jOutputInternalFrame.setVisible(true);
        jOutputInternalFrame.show();

    }//GEN-LAST:event_jExhaustiveTestingMenuItemActionPerformed

//    boolean fromEditeurToSys()
//    {
//        try {
//            sys = Analyseur.ouvrirStream(new ByteArrayInputStream(editor.getText().getBytes()));
//            setMenusEnabled(true);
//            return true;
//        } catch (ParseException ex) {
//            Logger.getLogger(Affich.class.getName()).log(Level.SEVERE, null, ex);JOptionPane.showMessageDialog(this, ex.toString(), TraductionPref.getMSG("ouvrir.probfich"), JOptionPane.ERROR_MESSAGE); return false;
//        } catch (FileNotFoundException ex) {
//            Logger.getLogger(Affich.class.getName()).log(Level.SEVERE, null, ex);JOptionPane.showMessageDialog(this, ex.toString(), TraductionPref.getMSG("ouvrir.probfich"), JOptionPane.ERROR_MESSAGE); return false;
//        } catch (TokenMgrError ex) {
//            Logger.getLogger(Affich.class.getName()).log(Level.SEVERE, null, ex);JOptionPane.showMessageDialog(this, ex.toString(), TraductionPref.getMSG("ouvrir.probfich"), JOptionPane.ERROR_MESSAGE); return false;
//        }
//
//    }
    private String fromXMLToString(File fichier) throws JDOMException, IOException {
        Element syst = (new SAXBuilder()).build(fichier).getRootElement();
        StringBuffer s = new StringBuffer();
        s.append("System " + syst.getAttributeValue("nom") + ";\n");
        s.append("Clocks ");
        boolean w = false;
        java.util.List<Element> li = syst.getChildren("Clock");
        for (Element elem : li) {
            if (w) {
                s.append(",");
            }
            w = true;
            s.append(elem.getText());
        }
        s.append(";\n");

        s.append("Inputs ");
        w = false;
        li = syst.getChildren("Input");
        for (Element elem : li) {
            if (w) {
                s.append(",");
            }
            w = true;
            s.append(elem.getText());
        }
        s.append(";\n");

        s.append("Outputs ");
        w = false;
        li = syst.getChildren("Output");
        for (Element elem : li) {
            if (w) {
                s.append(",");
            }
            w = true;
            s.append(elem.getText());
        }
        s.append(";\n");

        s.append("Locations ");
        w = false;
        li = syst.getChildren("Location");
        for (Element elem : li) {
            if (w) {
                s.append(",");
            }
            w = true;
            s.append(elem.getText());
        }
        s.append(";\n");

        s.append("Initial location " + syst.getAttributeValue("initialLocation") + ";\n");

        li = syst.getChildren("Transition");
        w = false;
        s.append("Transitions\n");
        for (Element elem : li) {
            if (w) {
                s.append("\n");
            }
            s.append("\t" + elem.getText() + ";");
            w = true;
        }
        s.append("\nend System;");
        return s.toString();
    }

    public void writeInto(JTextArea editor, TIOA tioa) throws java.io.IOException {
        Repository repository = Repository.getInstance();
        Collection<Clock> clocks = repository.getAllClocks();
        Iterator<Clock> iteratorClock;
        Collection<Location> locations = repository.getAllLocations();
        Iterator<Location> iteratorLocation;
        //java.io.PrintWriter fic=new java.io.PrintWriter(arg);

        editor.setText("");
        editor.append("System " + tioa.getName() + ";\r\nClocks ");

        editor.append(repository.getAllClocksToString());

        editor.append(";\r\nMessages ");
        editor.append(repository.getAllMessagesToString());
        editor.append(";\r\nLocations ");
        editor.append(repository.getAllLocationsToString());
        editor.append(";\r\nInitial location " + tioa.getInitialLocation() + ";\r\nTransitions\r\n");
        editor.append(";\r\nTransitions ");
        editor.append(repository.getAllTransitionsToString());
        editor.append("\r\nend System;");
        //fic.close();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Gui.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gui().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem exitMenuItem;
    private javax.swing.JButton jCancelToolParametersButton;
    private javax.swing.JTextArea jEditorTextArea;
    private javax.swing.JTextField jEpsilonTextField;
    private javax.swing.JMenuItem jExhaustiveTestingMenuItem;
    private javax.swing.JFrame jFrameOpenFile;
    private javax.swing.JMenuItem jHelpAboutMenuItem;
    private javax.swing.JMenuItem jIncrementalTestingMenuItem;
    private javax.swing.JTextField jInfinityTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu jMenuEdit;
    private javax.swing.JMenu jMenuFile;
    private javax.swing.JMenu jMenuHelp;
    private javax.swing.JMenu jMenuLanguage;
    private javax.swing.JMenu jMenuTest;
    private javax.swing.JMenu jMenuVerification;
    private javax.swing.JButton jOKToolParametersButton;
    private javax.swing.JFileChooser jOpenFileChooser;
    private javax.swing.JButton jOutputCloseButton;
    private javax.swing.JInternalFrame jOutputInternalFrame;
    private javax.swing.JButton jOutputSaveButton;
    private javax.swing.JTextArea jOutputTextArea;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jTIOAScrollPane;
    private javax.swing.JFrame jToolParametersFrame;
    private javax.swing.JMenuItem newMenuItem;
    private javax.swing.JMenuItem openMenuItem;
    private javax.swing.JMenuItem parametersMenuItem;
    private javax.swing.JMenuItem reachabilityMenuItem;
    private javax.swing.JMenuItem saveMenuItem;
    // End of variables declaration//GEN-END:variables

    private void setMenusEnabled(boolean b) {
        jMenuFile.setEnabled(b);
        jMenuTest.setEnabled(b);
        jMenuVerification.setEnabled(b);
        jMenuLanguage.setEnabled(b);
        jMenuHelp.setEnabled(b);
    }

    private void initFieldsValues() {
        jEpsilonTextField.setText(((Float) Parameters.getEpsilon()).toString());
        jInfinityTextField.setText(((Float) Parameters.getInfinity()).toString());
    }

    private void exit() {
        //System.exit(0);////////////////////////////////////////////////////////////////////////
        if (JOptionPane.showConfirmDialog(this,
                "Do you really want to exit this program ?",
                "Attention!",
                JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

    private void about() {
        JOptionPane.showMessageDialog(this, "TIOA V0.1", TraductionPref.getMSG("menu.about"),
                JOptionPane.INFORMATION_MESSAGE);
    }
}
